package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptSeriver;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> empPage(EmpQueryParam param) {
        // 创建分页对象
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());

        //根据更新时间倒序
        page.addOrder(OrderItem.desc("update_time"));

        //条件
//        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(param.getName() != null, Emp::getName, param.getName())
//                .eq(param.getGender() != null, Emp::getGender, param.getGender()).between(param.getBegin() !=null && param.getEnd() != null, Emp::getEntryDate,param.getBegin(), param.getEnd());
//        IPage<Emp> page1 = page(page, queryWrapper);
        //链式查询
        lambdaQuery().like(param.getName() != null, Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd()).page(page);
        //获取员工集合
        List<Emp> empList = page.getRecords();
        Set<Integer> deptIds = empList.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        //判断员工集合是否为空
        if (!CollectionUtils.isEmpty(empList)) {
            //根据部门id查询部门名称
            List<Dept> deptList = deptSeriver.listByIds(deptIds);
            //将部门集合转换为部门名称的Map
            if (!CollectionUtils.isEmpty(deptList)) {
                //创建部门名称的Map
                Map<Integer, String> deptMap = deptList.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
                //遍历员工集合，为每一个员工设置部门名称
                empList.forEach(emp -> emp.setDeptName(deptMap.get(emp.getDeptId())));
            }
        }
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    //新增员工
    @Override
    @Transactional
    public void saveEmpAndEmpExpr(Emp emp) {
        //保存员工基础信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);

        //保存员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr ->
                expr.setEmpId(emp.getId())
            );
            empExprMapper.insert(exprList);
        }
    }
//    根据id查询员工信息
    @Override
    public Emp getEmpById(Integer id) {
        //查询员工基础信息
        Emp emp = getById(id);
        //查询员工工作经历
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId, id);
        emp.setExprList(empExprMapper.selectList(queryWrapper));
        return emp;
    }

    //    修改员工信息
    @Override
    @Transactional
    public void updateEmp(Emp emp) {
        //修改员工基础信息
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        //删除员工工作经历
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmpExpr::getEmpId, emp.getId());
        empExprMapper.delete(queryWrapper);
        //保存员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr ->
                    expr.setEmpId(emp.getId())
            );
            empExprMapper.insert(exprList);
        }

    }
    //    删除员工信息
    @Override
    @Transactional
    public void removeEmpAndEmpExpr(List<Integer> ids) {
        //删除员工基础信息
        removeByIds(ids);
        //删除员工工作经历
        LambdaQueryWrapper<EmpExpr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EmpExpr::getEmpId, ids);
        empExprMapper.delete(queryWrapper);
    }
}
